package com.nail.soulda;

import java.io.BufferedReader;
import java.io.Console;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import com.nail.soulda.fighter.IFighter;
import com.nail.soulda.fighter.NormalFighter;
import com.nail.soulda.person.Boss;
import com.nail.soulda.person.IPerson;
import com.nail.soulda.person.SoulDa;

public class Main {
	private IPerson soulda = null, boss = null;	

	public static void main(String[] args) {
		Main main = new Main();
		main.loadData();		
	}
	
	public void loadData() {
		if((new File(FileStruct.SAVED_DATA)).exists()) {
			System.out.println("loading data from file");
			try {
				BufferedReader br = new BufferedReader(new FileReader(FileStruct.SAVED_DATA));
				this.soulda = new SoulDa();
				this.soulda.setData(br.readLine());
				this.boss = new Boss();
				this.boss.setData(br.readLine());
				
			} catch (FileNotFoundException e) {				
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else {
			Console console = System.console();
			this.soulda = new SoulDa(console.readLine("enter your name: "), Integer.parseInt(console.readLine("enter your sex(1:male, 0:female): ")));
			System.out.println("===============");
			this.boss = new Boss(console.readLine("enter boss name: "), Integer.parseInt(console.readLine("enter boss sex(1:male, 0:female): ")));
			this.saveData();
		}
		soulda.showInfo();
		boss.showInfo();
		IFighter fighter = new NormalFighter();
		fighter.fight(this.soulda, this.boss);
	}
	
	private void saveData() {
		try {
			FileWriter fw = new FileWriter(FileStruct.SAVED_DATA);			
			/* save  soulda */
			fw.write(this.soulda.toString());
			/* save boss */
			fw.append(this.boss.toString());
			fw.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
